package com.webwork.lims.transport.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.lims.common.controller.SuperCtrl;
import com.webwork.lims.transport.dataobject.Car;
import com.webwork.lims.transport.dataobject.CarStatus;
import com.webwork.lims.transport.dataobject.TransType;
import com.webwork.lims.transport.service.CarsService;
import com.webwork.lims.util.Paging;
import com.webwork.lims.util.StrManager;

@Controller
@RequestMapping(value = "/transport")
public class CarsController extends SuperCtrl {

	@Autowired
	private CarsService carSve;
	
	@RequestMapping(value = "/cars")
	@Override
	public String mainPage() {
		return "cars";
	}
	
	@RequestMapping(value = "queryCarsList")
	@ResponseBody
	public ResponseEntity<String> queryList(
			@RequestParam(value = "page") int page,
			@RequestParam(value = "rows") int rows,
			@RequestParam(required = false, value = "cmd") String cmdStr)
					throws Exception {
		Car queryObj = new Car();
		if (!StrManager.isEmpty(cmdStr)) {
			queryObj = new Gson().fromJson(cmdStr, Car.class);
			queryObj.setStatus(queryObj.getStatusCode());
			if (queryObj.getType() == null) {
				queryObj.setType(TransType.NULL);
			}
		}
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		ret.put("total", carSve.count(queryObj));
		ret.put("rows", carSve.query(queryObj, new Paging(page, rows)));
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "getCar")
	@ResponseBody
	public ResponseEntity<String> getCar(@RequestParam("carNum") String carNum) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Car car = carSve.get(carNum);
		return new ResponseEntity<String>(new Gson().toJson(car).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "addCar")
	@ResponseBody
	public String addCar(@RequestParam("jsonObj") String jsonObj, HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Car car = new Gson().fromJson(jsonObj, Car.class);
		if (carSve.get(car.getCarNum()) != null) {
			return EXIST;
		}
		car.setStatus(CarStatus.WAITING);
		carSve.save(car);
		return SUCCESS;
	}
	
	@RequestMapping(value = "editCar")
	@ResponseBody
	public String editCar(@RequestParam("jsonObj") String jsonObj) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Car c = new Gson().fromJson(jsonObj, Car.class);
		Car car = carSve.get(c.getCarNum());
		if (car == null) {
			return NOT_EXIST;
		}
		car.setCarNum(c.getCarNum());
		car.setZoneCode(c.getZoneCode());
		car.setType(c.getType());
		carSve.save(car);
		return SUCCESS;
	}
	
	@RequestMapping(value = "delCar")
	@ResponseBody
	public String delCar(@RequestParam("carNum") String carNum, HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Car car = carSve.get(carNum);
		if (car == null) {
			return FAIL;
		}
		carSve.delete(carNum);
		
		return SUCCESS;
	}
	
	@RequestMapping(value="loadCarStatus")
	@ResponseBody
	public ResponseEntity<String> loadStatus() {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		CarStatus[] status = CarStatus.useableValues();
		List<Object> ret = new ArrayList<Object>();
		for (CarStatus o : status) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", o.getCode());
			map.put("name", o.getCode() + " - " + o.getMsg());
			ret.add(map);
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
}
